#!/usr/bin/env python
import os
import subprocess
import sys

def run_command(cmd):
    """运行命令并返回结果"""
    try:
        result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
        return result.returncode, result.stdout, result.stderr
    except Exception as e:
        return -1, "", str(e)

def main():
    print("=== pyenv 诊断工具 ===\n")
    
    # 检查环境变量
    print("1. 检查环境变量:")
    pyenv_root = os.environ.get('PYENV_ROOT', '未设置')
    pyenv_home = os.environ.get('PYENV_HOME', '未设置')
    print(f"   PYENV_ROOT: {pyenv_root}")
    print(f"   PYENV_HOME: {pyenv_home}")
    
    # 检查pyenv命令
    print("\n2. 检查pyenv命令:")
    code, stdout, stderr = run_command("pyenv --version")
    if code == 0:
        print(f"   pyenv版本: {stdout.strip()}")
    else:
        print(f"   pyenv命令失败: {stderr}")
    
    # 检查已安装的Python版本
    print("\n3. 检查已安装的Python版本:")
    code, stdout, stderr = run_command("pyenv versions")
    if code == 0:
        print(f"   已安装版本:\n{stdout}")
    else:
        print(f"   获取版本列表失败: {stderr}")
    
    # 检查pyenv安装目录
    print("\n4. 检查pyenv安装目录:")
    pyenv_path = r"C:\Users\yongming.xu\.pyenv\pyenv-win"
    if os.path.exists(pyenv_path):
        print(f"   pyenv目录存在: {pyenv_path}")
        bin_path = os.path.join(pyenv_path, "bin")
        shims_path = os.path.join(pyenv_path, "shims")
        versions_path = os.path.join(pyenv_path, "versions")
        
        print(f"   bin目录: {'存在' if os.path.exists(bin_path) else '不存在'}")
        print(f"   shims目录: {'存在' if os.path.exists(shims_path) else '不存在'}")
        print(f"   versions目录: {'存在' if os.path.exists(versions_path) else '不存在'}")
    else:
        print(f"   pyenv目录不存在: {pyenv_path}")
    
    # 检查PATH
    print("\n5. 检查PATH环境变量:")
    path = os.environ.get('PATH', '')
    pyenv_in_path = 'pyenv' in path.lower()
    print(f"   PATH中包含pyenv: {'是' if pyenv_in_path else '否'}")
    
    # 尝试安装建议
    print("\n6. 安装建议:")
    print("   如果pyenv安装失败，可以尝试:")
    print("   - 使用预编译版本: pyenv install 3.9.2 --skip-existing")
    print("   - 或者下载离线安装包")
    print("   - 或者使用系统Python作为临时解决方案")

if __name__ == "__main__":
    main() 